<template>
  <div id="app">
    <div class="titleBar">
      <div class="title">
        <div class="logo">
          <img src="./assets/alpha.png" alt="omega.png" srcset="" />
        </div>
        <div class="txt">ELE-VUE</div>
      </div>
      <div class="windowTool">
        <div @click="minisize">
          <i class="iconfont">
            <img class="imgicon" src="./assets/down.png" />
          </i>
        </div>
        <div v-if="isMaxSize" @click="restore">
          <i class="iconfont">
            <img class="imgicon" src="./assets/restore.png" />
          </i>
        </div>
        <div v-else @click="maxsize">
          <i class="iconfont">
            <img class="imgicon" src="./assets/rectangle.png" />
          </i>
        </div>
        <div @click="close" class="close">
          <i class="iconfont">
            <img class="imgicon" src="./assets/close.png" />
          </i>
        </div>
      </div>
    </div>
    <div class="content">
      <router-view />
    </div>
  </div>
</template>

<script>
let { remote } = require("electron");

export default {
  name: "ele_vue",
  data() {
    return { isMaxSize: false };
  },
  mounted: function() {
    let win = remote.getCurrentWindow();
    win.on("maximize", () => {
      this.isMaxSize = true;
      this.setState();
    });
    win.on("unmaximize", () => {
      this.isMaxSize = false;
      this.setState();
    });
    win.on(
      "move",
      this.debounce(() => {
        this.setState();
      })
    );
    win.on(
      "resize",
      this.debounce(() => {
        this.setState();
      })
    );
    this.isMaxSize = win.isMaximized();
    this.getState();
  },
  methods: {
    close() {
      remote.getCurrentWindow().close();
    },
    minisize() {
      remote.getCurrentWindow().minimize();
    },
    restore() {
      remote.getCurrentWindow().restore();
    },
    maxsize() {
      remote.getCurrentWindow().maximize();
    },
    // 记录窗口状态
    setState() {
      let win = remote.getCurrentWindow();
      let rect = win.getBounds();
      let isMaxSize = win.isMaximized();
      let obj = { rect, isMaxSize };
      localStorage.setItem("winState", JSON.stringify(obj));
    },
    // 恢复窗口状态
    getState() {
      let win = remote.getCurrentWindow();
      let winState = localStorage.getItem("winState");
      if (winState) {
        winState = JSON.parse(winState);
        if (winState.isMaxSize) win.maximize();
        win.setBounds(winState.rect);
      }
      win.show();
    },
    // 防抖函数
    debounce(fn) {
      let timeout = null;
      return function() {
        clearTimeout(timeout);
        timeout = setTimeout((_) => {
          fn.apply(this, arguments);
        }, 300);
      };
    },
    // 限流函数
    throttle(fn) {
      let timeout = null;
      return function() {
        if (timeout) return;
        timeout = setTimeout((_) => {
          fn.apply(this, arguments);
          timeout = null;
        }, 300);
      };
    },
  },
};
</script>

<style>
/* CSS */
body,
html {
  margin: 0px;
  padding: 0px;
  pointer-events: none;
}
#app {
  box-sizing: border-box;
  height: 100%;
  width: 100%;
  border-radius: 200px;
  border: 1px solid #f5222f;
  background: #fff;
  overflow: hidden;
  pointer-events: auto;
  text-align: center;
  margin: 0px;
  padding: 0px;
  display: flex;
  flex-direction: column;
}
</style>

<style lang="scss" scoped>
.titleBar {
  height: 38px;
  line-height: 36px;
  background: #18f30480;
  display: flex;
  border-bottom: 1px sold #f5222f;

  .title {
    flex: 1;
    display: flex;
    -webkit-app-region: drag;

    .logo {
      padding-left: 8px;
      padding-right: 6px;
      img {
        width: 30px;
        height: 30px;
        margin-top: 7px;
      }
    }
  }
  .windowTool {
    border: 1px sold chartreuse;
    div {
      height: 100%;
      width: 38px;
      cursor: pointer;
      display: inline-block;
      i {
        font-size: 12px;
      }
      &:hover {
        background: #ff4d4f;
      }
    }
    .close:hover {
      color: #fff;
      background: #ff4d4f;
    }
  }
}
.imgicon {
  width: 30px;
  height: 30px;
  margin-top: 7px;
}
.iconfont {
  width: 30px;
  height: 30px;
}
.content {
  flex: 1;
  overflow-y: auto;
  overflow-x: auto;
}
</style>
